using System.Windows;
using System.Windows.Input;
using Silvery.GameControls;
using System;
using System.Windows.Controls;

namespace Silvery
{
    public interface IGameRoot : IBaseObjCollection
    {
        IGameAssembly GameAssembly { get; }
    }
    /// <summary>
    /// 游戏的根
    /// </summary>
    public class GameRoot : GameAssembly, IGameRoot
    {
        
        /// <summary>
        /// 构造函数
        /// </summary>
        public GameRoot()
        {
            Current = this;
            createGameRoot(this);
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="root">从外部传入根，比如嵌套在其他的程序里的时候就非常有用了</param>
        public GameRoot(FrameworkElement root)
        {
            Current = this;
            createGameRoot(root);
        }
        void createGameRoot(FrameworkElement root)
        {
            GameCommands.WriteLineToOutput(DateTime.Now.ToString() + ":" + this.ClassName + "(Gameroot)创建");
            gameroot = root;
            gameroot.KeyDown += new KeyEventHandler(Game_KeyDown);
            GameRandom = new Random((int)DateTime.Now.Ticks);

            // The Settings object, which represents Web browser settings.
            System.Windows.Interop.Settings settings = Application.Current.Host.Settings;

            // Read/write properties of the Settings object.
            //settings.EnableFrameRateCounter = true;
            //settings.EnableRedrawRegions = true;
            settings.MaxFrameRate = 60;
            

        }

        public Random GameRandom { get; private set; }
        /// <summary>
        /// 按键按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void Game_KeyDown(object sender, KeyEventArgs e)
        {
            if (IsLockHotkeyInput == false)
            {
                if (HotKeys.IsEnabledHotKeys)
                {
                    ModifierKeys mkeys = Keyboard.Modifiers;

                    var hotkey = HotKeys.Checking(e.Key, mkeys);
                    if (hotkey != null)
                        hotkey.ActivatedEvent();
                }
            }
        }
        /// <summary>
        /// 按键抬起事件,只对单键有效，因为组合键是两个以上的
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public virtual void Game_KeyUp(object sender, KeyEventArgs e)
        {
            if (IsLockHotkeyInput == false)
            {
                if (HotKeys.IsEnabledHotKeys)
                {
                    var hotkey = HotKeys.Checking(e.Key);
                    if (hotkey != null)
                        hotkey.ActivatedEvent();
                }
            }
        }
        /// <summary>
        /// 游戏组件类
        /// </summary>
        public IGameAssembly GameAssembly { get { return this; } }
        private FrameworkElement gameroot;
        /// <summary>
        /// 游戏的根
        /// </summary>
        public static IGameRoot Current { get; private set; }
        /// <summary>
        /// 游戏元素集合
        /// </summary>
        public UIElementCollection GameObjects { get { return this.Children; } }

        /// <summary>
        /// 添加基本物体到屏中
        /// </summary>
        /// <param name="obj"></param>
        public virtual void AddBaseObj(BaseObject obj)
        {
            this.Children.Insert(Children.Count-1, obj);
        }
        /// <summary>
        /// 移除基本物体从屏中
        /// </summary>
        /// <param name="obj"></param>
        public virtual void RemoveBaseObj(BaseObject obj)
        {
            this.Children.Remove(obj);
        }
        /// <summary>
        /// 一个简单的统计
        /// </summary>
        public virtual int BaseObjectsCount
        {
            get { return Children.Count; }
        }
        public override void Dispose()
        {
            if (gameroot != null)
            {
                gameroot.KeyDown -= Game_KeyDown;
                gameroot.KeyUp -= Game_KeyUp;
                gameroot = null;
            }
            base.Dispose();
        }
    }
}
